package cn.cnsuh.controller.web.servlet;


import cn.cnsuh.domain.Game;
import cn.cnsuh.domain.PageBean;
import cn.cnsuh.service.GameService;
import cn.cnsuh.service.impl.GameServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Magic
 * @create 2020-05-18 22:59
 */
@WebServlet("/game/*")
public class GameServlet extends BaseServlet  {

//    private RouteService routeService = new RouteServiceImpl();
    private GameService gameService = new GameServiceImpl();

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        //2.接收游戏名称

        String gname = request.getParameter("gname");
//
//        gname = new String(gname.getBytes("iso-8859-1"),"utf-8");


        //处理参数
        int currentPage = 0;//当前页面，如果不传递，默认为1
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            currentPage = 1;
        }

        int pageSize = 0;//每页显示条数，如果不传递默认显示5条记录
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            pageSize = 5;
        }

        //3.调用service查询PageBean对象
//        PageBean<Route> pb = routeService.pageQuery(cid, currentPage, pageSize,rname);
        PageBean<Game> pb =gameService.pageQuery(currentPage,pageSize,gname);


        //4.将PageBean对象序列化为json，返回
        writeValue(pb, response);
    }
}
